package com.shycloud.mido.member.service.impl;

import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.AtvMainActive;
import com.shycloud.mido.member.api.entity.MiniBanner;
import com.shycloud.mido.member.api.util.Constant;
import com.shycloud.mido.member.api.vo.MiniBannerVO;
import com.shycloud.mido.member.mapper.MiniBannerMapper;
import com.shycloud.mido.member.service.MiniBannerService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 小程序banner管理 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2021-06-18
 */
@Service
public class MiniBannerServiceImpl extends ServiceImpl<MiniBannerMapper, MiniBanner> implements MiniBannerService {

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveOrUpdateMiniBanner(MiniBannerVO vo) {
		if (Objects.isNull(SecurityUtils.getUser())) {
    		throw new BusinessException("用户必须登录");
		}
		Integer systemUserId = SecurityUtils.getUser().getId();
		MiniBanner miniBanner = new MiniBanner();
		if (StringUtils.isBlank(vo.getId())) {
			//添加
			BeanUtils.copyProperties(vo, miniBanner);
			
			miniBanner.setStatus("0");//启用
			miniBanner.setDelFlag(Constant.DEL_FLAG);
			miniBanner.setCreateUser(String.valueOf(systemUserId));
			miniBanner.setCreateTime(LocalDateTime.now());
		}else {
			miniBanner = super.getById(vo.getId());
			if (Objects.isNull(miniBanner)) {
				throw new BusinessException(vo.getId()+"数据不存在");
			}
			//更新
			miniBanner.setLink(vo.getLink());
			miniBanner.setCategoryId(vo.getCategoryId());
			miniBanner.setImgUrl(vo.getImgUrl());
			miniBanner.setTitle(vo.getTitle());
			miniBanner.setLinkMethod(vo.getLinkMethod());
			
		}
		miniBanner.setUpdateUser(String.valueOf(systemUserId));
		miniBanner.setUpdateTime(LocalDateTime.now());
		super.saveOrUpdate(miniBanner);
		return false;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean editStatus(MiniBannerVO vo) {
		if (Objects.isNull(SecurityUtils.getUser())) {
    		throw new BusinessException("用户必须登录");
		}
		Integer systemUserId = SecurityUtils.getUser().getId();
		LambdaUpdateWrapper<MiniBanner> updateWrapper = Wrappers.<MiniBanner>lambdaUpdate()
				.set(MiniBanner::getUpdateUser, String.valueOf(systemUserId))
				.set(MiniBanner::getUpdateTime, LocalDateTime.now())
				.set(MiniBanner::getStatus, vo.getStatus())
				.eq(MiniBanner::getId, vo.getId());
		super.update(updateWrapper);
		return false;
	}

}
